package com.shijiwei.lifthelper.ui;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.adapter.CookCategoryAdapter;
import com.shijiwei.lifthelper.bean.CookBean;
import com.shijiwei.lifthelper.bean.CookData;
import com.shijiwei.lifthelper.bean.CookInfo;
import com.shijiwei.lifthelper.utils.UrlConstants;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_cook_category)
public class CookCategoryActivity extends AppCompatActivity {

    @ViewInject(R.id.iv_cook_back_top)
    private ImageView backToTopImage;
    @ViewInject(R.id.layout_loading_cook)
    private FrameLayout loadingLayout;
    @ViewInject(R.id.tv_cook_category)
    private TextView titleText;
    @ViewInject(R.id.iv_cook_close)
    private ImageView closeImage;
    @ViewInject(R.id.ptrlv_cook)
    private PullToRefreshListView cookPtrListView;

    private CookCategoryAdapter cookCategoryAdapter;
    private List<CookData> datas = new ArrayList<>();
    private Callback.Cancelable  get;

    private String category;
    private int start = 0;

    // 声明一个动画
    private AnimationDrawable drawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cook_category);
        x.view().inject(this);
        category = getIntent().getStringExtra("category");
        titleText.setText(category);

        ImageView iv = (ImageView) loadingLayout.findViewById(R.id.iv_loading);
        if (iv.getBackground() instanceof AnimationDrawable) {
            drawable = (AnimationDrawable) iv.getBackground();
        }
        if (drawable != null) {
            drawable.start();
        }

        initView();
        initData();
    }

   private void initView() {
       closeImage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               finish();
           }
       });

       cookCategoryAdapter = new CookCategoryAdapter(datas,this);
       cookPtrListView.setAdapter(cookCategoryAdapter);

       cookPtrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
       cookPtrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
           @Override
           public void onRefresh(PullToRefreshBase<ListView> refreshView) {
               start += 15;
               get = x.http().get(getRequestParams(), new Callback.CommonCallback<String>() {
                   @Override
                   public void onSuccess(String result) {
                       LogUtil.e("----------CookBook" + result);
                       if (result != null) {
                           CookBean cookBean = JSONObject.parseObject(result, CookBean.class);
                           if (cookBean != null) {
                               List<CookData> cookDatas = cookBean.getResult().getData();
                               datas.addAll(cookDatas);
                               cookCategoryAdapter.notifyDataSetChanged();
                           } else {
                               Toast.makeText(CookCategoryActivity.this, "网络异常，请求不到数据啊！！", Toast.LENGTH_SHORT).show();
                           }
                       } else {
                           Toast.makeText(CookCategoryActivity.this, "网络异常，请求不到数据啊！！", Toast.LENGTH_SHORT).show();
                       }
                   }

                   @Override
                   public void onError(Throwable ex, boolean isOnCallback) {
                       Toast.makeText(CookCategoryActivity.this, "网络异常，请求不到数据啊！！", Toast.LENGTH_SHORT).show();
                   }

                   @Override
                   public void onCancelled(CancelledException cex) {

                   }

                   @Override
                   public void onFinished() {

                   }
               });
           }
       });

       cookPtrListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Intent intent = new Intent(CookCategoryActivity.this, CookInfoActivity.class);
               intent.putExtra("data", datas.get(position - 1));
               startActivity(intent);
           }
       });

       cookPtrListView.setOnScrollListener(new AbsListView.OnScrollListener() {
           @Override
           public void onScrollStateChanged(AbsListView view, int scrollState) {
           }

           @Override
           public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
               if (firstVisibleItem > 3) {
                   backToTopImage.setVisibility(View.VISIBLE);
               } else if (firstVisibleItem == 0) {
                   backToTopImage.setVisibility(View.GONE);
               }
           }
       });

       backToTopImage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               cookPtrListView.getRefreshableView().setSelection(0  );
           }
       });
    }

    private void initData() {
        backToTopImage.setVisibility(View.GONE);
        get = x.http().get(getRequestParams(), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.e("----------CookBook"+result);
                if (result != null){
                    CookBean cookBean = JSONObject.parseObject(result, CookBean.class);
                    if (cookBean != null) {
                        List<CookData> cookDatas = cookBean.getResult().getData();
                        datas.addAll(cookDatas);
                        cookCategoryAdapter.notifyDataSetChanged();
                        loadingLayout.setVisibility(View.GONE);
                    } else {
                        Toast.makeText(CookCategoryActivity.this, "网络异常，请求不到数据啊！！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(CookCategoryActivity.this,"网络异常，请求不到数据啊！！",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(CookCategoryActivity.this,"网络异常，请求不到数据啊！！",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private RequestParams getRequestParams(){
        RequestParams params = new RequestParams(UrlConstants.COOKBOOK_URL_GET);
        params.addBodyParameter("menu",category);
        params.addBodyParameter("rn","15");
        params.addBodyParameter("start",start+"");
        return params;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        get.cancel();
        if (drawable != null){
            drawable.start();
        }
    }
}
